<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 2018/9/25
 * Time: 16:35
 */

namespace common\models\db;

use common\components\tools\ArrayHelper;
use common\members\wodrow\tools\Tree;


/**
 * Class Area
 * @package common\models\db
 *
 * @property Area $f
 * @property Area[] $areas
 * @property Area[] $childAreas
 * @property array $info
 * @property array $allParents
 * @property array $allParentsIds
 * @property string $allParentsNames
 */
class Area extends \common\models\db\tables\Area
{
    const CHINA_AREA_ROOT = 4744;

    /**
     * @return array
     */
    public function getInfo()
    {
        $r = [];
        return $r;
    }

    /**
     * @return array|Area[]|mixed|\yii\db\ActiveRecord[]
     */
    public static function getAllAreas($as_arr = 0)
    {
        ini_set("memory_limit","-1");
        $key = "all_jd_area_".$as_arr."_".date("Ymd");
        $all = \Yii::$app->cache->get($key);
        if (!$all){
            if ($as_arr){
                $all = self::find()->asArray()->all();
            }else{
                $all = self::find()->all();
            }
            \Yii::$app->cache->set($key, $all, 3600*24);
        }
        return $all;
    }

    /**
     * @return array|Area[]|mixed|\yii\db\ActiveRecord[]
     */
    public static function getAllAreaTree()
    {
        ini_set("memory_limit","-1");
        $key = "all_jd_area_tree"."_".date("Ymd");
        $tree = \Yii::$app->cache->get($key);
        if (!$tree){
            $all = self::getAllAreas(1);
            $tree = Tree::list2tree($all, 0, 'id', 'fid', 'childAreas');
            \Yii::$app->cache->set($key, $tree, 3600*24);
        }
        return $tree;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getF()
    {
        return $this->hasOne(self::class, ['id' => 'fid']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAreas()
    {
        return $this->hasMany(self::class, ['fid' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getChildAreas()
    {
        return $this->hasMany(self::class, ['fid' => 'id']);
    }

    /**
     * @return array
     */
    public function getAllParents()
    {
        $p = [];
        $f = $this->f;
        if ($f){
            $p[] = $f;
            $p = ArrayHelper::merge($p, $f->getAllParents());
        }
        return $p;
    }

    /**
     * @return array
     */
    public function getAllParentsIds()
    {
        $ids = [];
        $f = $this->f;
        if ($f){
            $ids[] = $f->id;
            $ids = ArrayHelper::merge($ids, $f->getAllParentsIds());
        }
        return $ids;
    }

    /**
     * @param string $separator
     * @return array|string
     */
    public function getAllParentsNames($separator = " ")
    {
        $ids = $this->allParentsIds;
        $ids = array_reverse($ids);
        $names = [];
        foreach ($ids as $k => $v){
            $area = self::findOne(['id' => $v]);
            if ($area)$names[] = $area->name;
        }
        $names = ArrayHelper::arr2str($names, $separator);
        return $names;
    }
}